package com.sojson.util.security.springsecurity.service.impl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.sojson.result.Result;
import com.sojson.util.StringUtil;
import com.sojson.util.servlet.ServletUtil;
import com.sojson.util.token.Token;
import com.sojson.util.token.TokenUtil;

/**
 * 自定义退出处理类 返回成功
 * 
 * @author liu
 * @date 2021-04-14
 */
@Configuration
public class LogoutServiceImpl implements LogoutSuccessHandler {

    /**
     * 退出处理
     *
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
        throws IOException, ServletException {
        Token token = TokenUtil.getToken(false);
        if (StringUtil.isNotBlankObject(token)) {
            // 删除用户缓存记录
            TokenUtil.removeToken(token.getId());
        }

        ServletUtil.out(response, Result.success());
    }

}